package chapter04;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestCorrectPostureStep1 {
    private static final Object ROOM=new Object();
    private static boolean hasCigarette=false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            synchronized (ROOM){
                log.debug("有烟没？【{}】",hasCigarette);
                if(!hasCigarette){
                    log.debug("没有烟，歇一会");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有烟没？【{}】",hasCigarette);
                if (hasCigarette){
                    log.debug("有烟了，开始干活");
                }
            }
        },"小南").start();
        for (int i=1;i<=5;i++){
            new Thread(()->{
                synchronized (ROOM){
                    log.debug("开始干活了...");
                }
            },"其他人"+i).start();
        }
        Thread.sleep(1000);
        new Thread(()->{
            log.debug("烟到了哦~");
            hasCigarette=true;
        },"送烟的人").start();
    }
}
